package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.ReturnReasonMapper;
import com.tanhua.model.domain.OmsOrderReturnReason;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;

@DubboService
public class ReturnReasonApiImpl implements ReturnReasonApi {

    @Autowired
    private ReturnReasonMapper returnReasonMapper;

    @Override
    public Page listReturnReason(Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnReason> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        return returnReasonMapper.selectPage(page, wrapper);
    }

    /**
     * 修改退货原因启用状态
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    public boolean updateStatus(Integer status, Integer[] ids) {
        if (status == null) {
            return false;
        }
        for (Integer id : ids) {
            OmsOrderReturnReason reason = new OmsOrderReturnReason();
            reason.setId(Long.valueOf(status));
            reason.setStatus(id);
            returnReasonMapper.updateById(reason);
        }
        return true;
    }

    /**
     * 修改退货原因
     *
     * @param id
     * @param reason
     * @return
     */
    @Override
    public boolean updateById(Integer id, OmsOrderReturnReason reason) {
        if (reason == null) {
            return false;
        }
        returnReasonMapper.updateById(reason);
        return true;
    }

    /**
     * 获取单个退货原因详情信息
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrderReturnReason getById(Integer id) {
        if (id == null) {
            return null;
        }
        return returnReasonMapper.selectById(id);
    }

    /**
     * 批量删除退货原因
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deletes(Integer[] ids) {
        if (ids.length == 0) {
            return false;
        }
        returnReasonMapper.deleteBatchIds(Arrays.asList(ids));
        return true;
    }

    /**
     * 添加退货原因
     *
     * @param reason
     * @return
     */
    @Override
    public boolean create(OmsOrderReturnReason reason) {
        if (reason == null) {
            return false;
        }
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrderReturnReason::getName, reason.getName());
        if (returnReasonMapper.selectCount(wrapper) > 0) {
            throw new BusinessException(ErrorResult.resourceError());
        }
        reason.setCreateTime(new Date(System.currentTimeMillis()));
        returnReasonMapper.insert(reason);
        return true;
    }
}
